package com.keyroy.game.puzzle_rpg.dialogs;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;

import com.keyroy.game.puzzle_rpg.models.interfaces.EventListener;
import com.keyroy.game.puzzle_rpg.models.skill.Skill;
import com.keyroy.game.puzzle_rpg.utils.FileUtil;
import com.keyroy.game.puzzle_rpg.views.IconView;
import com.keyroy.game.puzzle_rpg.views.tables.CostTableView;
import com.keyroy.game.puzzle_rpg.views.tables.EffectTableView;
import com.keyroy.game.puzzle_rpg.views.tables.SkillCostTableView;

public class SkillEditor extends Shell {
	private Text skill_name;
	private Text skill_icon;
	private Text skill_info;
	private SkillCostTableView skill_cost_table_view;
	private IconView canvas;
	private EffectTableView effect_table_view;
	private CostTableView cost_table_view;
	private Combo skill_scope;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			SkillEditor shell = new SkillEditor(display, null);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the shell.
	 * 
	 * @param display
	 */
	public SkillEditor(Display display, final EventListener<Skill> listener) {
		super(display, SWT.DIALOG_TRIM);

		canvas = new IconView(this);
		canvas.setBounds(10, 10, 100, 100);

		Label lblNewLabel = new Label(this, SWT.NONE);
		lblNewLabel.setBounds(116, 13, 35, 17);
		lblNewLabel.setText("\u540D\u79F0 : ");

		Label label = new Label(this, SWT.NONE);
		label.setText("\u56FE\u6807 : ");
		label.setBounds(116, 47, 35, 17);

		skill_name = new Text(this, SWT.BORDER);
		skill_name.setBounds(157, 10, 277, 23);

		skill_icon = new Text(this, SWT.BORDER);
		skill_icon.setBounds(157, 44, 191, 23);

		Button btnNewButton = new Button(this, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String path = FileUtil.selectFile(getShell(), null, null, new String[] { "*.png", "*.gif" });
				if (path != null) {
					skill_icon.setText(path);
				}
				canvas.setImage(path);
			}
		});
		btnNewButton.setBounds(354, 42, 80, 27);
		btnNewButton.setText("\u9009\u62E9\u6587\u4EF6");

		TabFolder tabFolder = new TabFolder(this, SWT.NONE);
		tabFolder.setBounds(10, 116, 424, 263);

		TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
		tabItem.setText("\u6548\u679C");

		effect_table_view = new EffectTableView(tabFolder);
		tabItem.setControl(effect_table_view);

		TabItem tabItem_1 = new TabItem(tabFolder, SWT.NONE);
		tabItem_1.setText("\u6D88\u8017");

		cost_table_view = new CostTableView(tabFolder);
		tabItem_1.setControl(cost_table_view);

		TabItem tabItem_2 = new TabItem(tabFolder, SWT.NONE);
		tabItem_2.setText("\u6280\u80FD\u6D88\u8017");

		skill_cost_table_view = new SkillCostTableView(tabFolder);
		tabItem_2.setControl(skill_cost_table_view);
		
		TabItem tabItem_3 = new TabItem(tabFolder, SWT.NONE);
		tabItem_3.setText("\u7B80\u4ECB");
		
				skill_info = new Text(tabFolder, SWT.BORDER);
				tabItem_3.setControl(skill_info);

		Button btnNewButton_1 = new Button(this, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (listener != null) {
					Skill skill = new Skill();
					skill.name = skill_name.getText();
					skill.icon = skill_icon.getText();
					skill.info = skill_info.getText();
					skill.effect = effect_table_view.getEffect();
					skill.cost = cost_table_view.getCost();
					skill.skillCost = skill_cost_table_view.getSkillCost();
					skill.scope = skill_scope.getSelectionIndex();
					listener.onEvent(skill);
				}
				dispose();
			}
		});
		btnNewButton_1.setBounds(354, 385, 80, 27);
		btnNewButton_1.setText("\u5B8C\u6210");

		Label lblNewLabel_2 = new Label(this, SWT.NONE);
		lblNewLabel_2.setBounds(10, 390, 47, 17);
		lblNewLabel_2.setText("\u4F5C\u7528\u57DF : ");

		skill_scope = new Combo(this, SWT.READ_ONLY);
		skill_scope.setItems(new String[] { "\u654C\u4EBA-\u5355\u4F53", "\u654C\u4EBA-\u6240\u6709",
				"\u53CB\u65B9-\u5355\u4F53", "\u53CB\u65B9-\u6240\u6709" });
		skill_scope.setBounds(63, 387, 172, 25);
		skill_scope.select(0);
		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("\u6280\u80FD\u7F16\u8F91");
		setSize(450, 450);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

}
